package io.renren.common.utils;

import java.math.RoundingMode;
import java.text.NumberFormat;

public class TextUtils {
    public static String formatDouble(double d){
        NumberFormat nf = NumberFormat.getNumberInstance();
        //保留两位小数
        nf.setMaximumFractionDigits(2);
        //如果不需要四舍五入可以使用RoundingMode.DOWN
        nf.setRoundingMode(RoundingMode.UP);
        return nf.format(d);
    }
    public static String formatFileSize(long tmpSize){
        if (tmpSize < 1024) {
            return tmpSize+"B";
        } else if (tmpSize >= 1024 && tmpSize < 1048576) {
            return formatDouble(tmpSize/ 1024f)+"KB";
        } else if (tmpSize > 1048576) {
            return formatDouble(tmpSize/ 1048576f)+"MB";
        }else{
            return String.valueOf(tmpSize);
        }
    }
}
